package practice.MyProject.util;

import java.util.Scanner;

/**
 * @Author juzipi
 * @Date 2021/6/25 10:13
 * @Info
 */
public class CustomUtil {

    public static Scanner scanner = new Scanner(System.in);


    public static char readMenuSelection(){
        char cha;
        for (;;){
            String s = readKeyBoard(1, false);
            cha = s.charAt(0);
            if (cha != '1' && cha != '2' && cha != '3' && cha != '4' && cha != '5' && cha != '6'){
                System.out.println("未找到此项操作，请重新选择");
            }else {
                break;
            }
        }
        return cha;
    }

    private static String readKeyBoard(int limit,boolean blankReturn){
        String line = "";
        //获取scanner监听的值
        while (scanner.hasNextLine()){
            line = scanner.nextLine();
            //如果空就返回空，否则就跳过
            if (line.length() == 0){
                if (blankReturn) {
                    return line;
                }else {
                    break;
                }
            }

            if (line.length() > limit){
                System.out.println("输入长度过大 最大长度为"+limit+"，请重新输入");
                continue;
            }
            break;
        }
        return line;
    }

}
